import 'package:flutter/material.dart';

class OrderInfoCard extends StatelessWidget {
  final String orderTime;
  final String orderStatus;
  final String productName;
  final String productSpec;
  final double unitPrice;
  final double totalPrice;
  final String imageUrl;
  final VoidCallback? onRepurchase;

  const OrderInfoCard({
    super.key,
    required this.orderTime,
    required this.orderStatus,
    required this.productName,
    required this.productSpec,
    required this.unitPrice,
    required this.totalPrice,
    required this.imageUrl,
    this.onRepurchase,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.all(16),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 订单时间和状态
            _buildOrderHeader(),
            const SizedBox(height: 16),

            // 商品信息（包含图片）
            _buildProductInfo(),
            const SizedBox(height: 16),

            // 合计金额
            _buildTotalPrice(),
            const SizedBox(height: 16),

            // 再次购买按钮
            _buildRepurchaseButton(),
          ],
        ),
      ),
    );
  }

  Widget _buildOrderHeader() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          orderTime,
          style: const TextStyle(color: Colors.grey),
        ),
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
          decoration: BoxDecoration(
            color: _getStatusColor(orderStatus).withOpacity(0.2),
            borderRadius: BorderRadius.circular(12),
          ),
          child: Text(
            orderStatus,
            style: TextStyle(color: _getStatusColor(orderStatus)),
          ),
        ),
      ],
    );
  }

  Widget _buildProductInfo() {
    return Row(
      children: [
        // 商品图片
        ClipRRect(
          borderRadius: BorderRadius.circular(8),
          child: Image.network(
            imageUrl,
            width: 80,
            height: 80,
            fit: BoxFit.cover,
            errorBuilder: (context, error, stackTrace) {
              return Container(
                width: 80,
                height: 80,
                color: Colors.grey[200],
                child:
                    const Icon(Icons.image_not_supported, color: Colors.grey),
              );
            },
          ),
        ),
        const SizedBox(width: 16),
        // 商品信息
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                productName,
                style: const TextStyle(fontSize: 16),
              ),
              const SizedBox(height: 8),
              Text(
                '规格：$productSpec',
                style: TextStyle(color: Colors.grey[600]),
              ),
              const SizedBox(height: 8),
              Text(
                '¥${unitPrice.toStringAsFixed(2)}',
                style: const TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                  color: Colors.red,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildTotalPrice() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        const Text('合计：'),
        const SizedBox(width: 8),
        Text(
          '¥${totalPrice.toStringAsFixed(2)}',
          style: const TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
            color: Colors.red,
          ),
        ),
      ],
    );
  }

  Widget _buildRepurchaseButton() {
    return SizedBox(
      width: double.infinity,
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          padding: const EdgeInsets.symmetric(vertical: 16),
        ),
        onPressed: onRepurchase,
        child: const Text('再次购买'),
      ),
    );
  }

  Color _getStatusColor(String status) {
    switch (status) {
      case '待发货':
        return Colors.orange;
      case '已发货':
        return Colors.blue;
      case '已完成':
        return Colors.green;
      default:
        return Colors.grey;
    }
  }
}
